package mis.ds.ps.model.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import mis.ds.ps.model.dao.DocentesDao;
import mis.ds.ps.model.obj.Docentes;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl;
//import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.PreparedStatement;
//import com.mysql.jdbc.Statement;

/**
 * DAO for table: Docentes.
 * 
 * @author autogenerated
 */
@Repository
public class DocentesDaoImpl extends GenericHibernateDaoImpl<Docentes, Long> implements DocentesDao {
	
	public static final int ID = 1;
	public static final int CODIGO = 3;
	public static final int SIGLA = 4;
	public static final int NOME = 5;

	private static Logger logger = Logger.getLogger(DocentesDaoImpl.class);

	private Connection mysqlConn;

	/** Constructor method. */
	public DocentesDaoImpl() {
		super(Docentes.class);
	}
	
	public List<Docentes> getDocentes() {
		
		List<Docentes> lstDocentes = new ArrayList<Docentes>();
		
		String sql = "SELECT * from docentes";
		System.out.println(sql);

//		PreparedStatement prest;
		Statement stmt;
		try {
//			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
//			logger.debug("SQL select statement [" + prest.toString() + "]");
//			ResultSet rs = prest.executeQuery();
				
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/initial_timetable","root","12345");
			stmt = (Statement) con.createStatement();
			logger.debug("SQL select statement [" + stmt.toString() + "]");
			ResultSet rs = stmt.executeQuery(sql);
			
			while (rs.next()) {
				Docentes dc = new Docentes(Long.valueOf(rs.getString(ID)));
				dc.setCodigo(rs.getInt(CODIGO));
				dc.setSigla(rs.getString(SIGLA));
				dc.setNome(rs.getString(NOME));	
				System.out.println(rs.getString(NOME));
				
				lstDocentes.add(dc);
			}
			System.out.println(lstDocentes.size());
		} catch (SQLException e1) {
			System.out.println(e1.getMessage());
			logger.debug("Cannot get Docentes from DB ", e1);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lstDocentes;

	}
	
//	public List<SelectItem> getRespostasPergunta1() {  
//        Session session = HibernateUtil.currentSession();  
//        RespostasDao dao = new RespostasDao(session, Respostas.class);  
//        List<Respostas> respostas = dao.getRespostasByPergunta1(new Long(1));  
//        List<SelectItem> items = new ArrayList<SelectItem>(respostas.size());  
//        for (Respostas r : respostas) {  
//            items.add(new SelectItem(r.getCodRespostas(), r.getDescricao()));  
//        }  
//        return items;  
//  
//    }  

	public boolean insertDocente(Docentes docente) {
		return false;
	}

	public int insertDocentes(List<Docentes> docentes) {

		int totalInsert = 0;
		String sql = "INSERT docentes VALUES(?)";
		PreparedStatement prest;
		try {
			for (Iterator<Docentes> it = docentes.iterator(); it.hasNext();) {
				Docentes docente = it.next();

				prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
				prest.setString(1, docente.getNome());
				int count = prest.executeUpdate();
				totalInsert += count;
			}
			System.out.println("Inseridos " + totalInsert + " no total");
		} catch (SQLException e1) {
			logger.debug("Erro ao executar o comando de sql: " + sql, e1);
		}

		return 0;
	}

	public boolean updateDocente() {
		return false;
	}

	public boolean removeDocente() {
		return false;
	}
	
	public Connection getMysqlConn() {
		return mysqlConn;
	}

	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}
}
